package cn.easthome.demo.day0060_pm.dao;

import cn.easthome.demo.day0060_pm.util.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IJobInfoDao接口的实现类
 */
public class JobInfoDaoImpl implements IJobInfoDao{

    @Override
    public Object getCenter2Data() {
        //从数据库查询center2的数据
        DataSource dataSource = JDBCUtil.getDataSource();
        QueryRunner runner = new QueryRunner(dataSource);
        //借助runner执行sql操作
        String sql = "select 省份, count(*) from job_info group by 省份";
        List<Map<String, Object>> lists = null;
        List<Map<String, Object>> new_data = new ArrayList<>();
        try {
            // {省份=广东, count(*)=3245},
            // {"name": '河南', "value": 356}
            lists = runner.query(sql, new MapListHandler());
            System.out.println(lists);
            for (Map<String, Object> map: lists) {
                Map<String, Object> new_map = new HashMap<>();
                new_map.put("name", map.get("省份"));
                new_map.put("value", map.get("count(*)"));
                new_data.add(new_map);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return new_data;
    }
}
